package com.applet.message.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.applet.code.enums.SmsTypeEnum;

import com.applet.message.sms.service.SmsService;
import com.applet.message.sms.req.SmsSendRequest;
import com.applet.message.sms.req.SmsVariableRequest;
import com.applet.message.sms.resp.SmsSendResponse;
import com.applet.message.sms.resp.SmsVariableResponse;
import com.applet.message.util.HttpUtils;
import com.applet.message.util.SMSUtil;
import com.applet.code.vo.SmsReturnVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 短信实现类
 */
@Service
public class SmsServiceImpl implements SmsService {
    private Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);
    @Value("${sms.url.send}")
    private String sendUrl;
    @Value("${sms.url.variableSend}")
    private String variableSend;
    @Value("${sms.account}")
    private String account;
    @Value("${sms.password}")
    private String password;
    //状态报告
    private static String report = "true";

    /**
     * TODO 没有对接国际的
     *
     * @param type  短信类型
     * @param phone 多个手机号码使用英文逗号分隔
     * @return
     */
    @Override
    public SmsReturnVo sendSms(SmsTypeEnum type, String phone) {
        String code = SMSUtil.getSmsCode(6);
        String msg = "【ISTPOS】验证码:+ code 30分钟内有效，请勿向其他人泄露";
        msg = msg.replace("+ code", code);
        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, msg, phone, report);
        String requestJson = JSONObject.toJSONString(smsSingleRequest);
        logger.info("sendSms before request string is: " + requestJson);
        String response = HttpUtils.doPostHttp(sendUrl, null, requestJson);
        SmsSendResponse smsSingleResponse = JSONObject.parseObject(response, SmsSendResponse.class);

        SmsReturnVo smsReturnVo = new SmsReturnVo();
        smsReturnVo.setCode(code);
        smsReturnVo.setContext(msg);
        smsReturnVo.setIsSuccess(false);
        if (smsSingleRequest != null && smsSingleResponse.getCode().equals("0")) {
            smsReturnVo.setIsSuccess(true);
            return smsReturnVo;
        }
        logger.info("sendSms response after request result is :" + response);
        return smsReturnVo;

    }

    /**
     * 发送变量短信
     *
     * @param type   短信类型
     * @param params 手机号码和变量参数，多组参数使用英文分号;区分
     *               15800000000,张先生,1234;13800000000,李小姐,4321
     * @return
     */
    @Override
    public SmsReturnVo smsVariable(SmsTypeEnum type, String params) {
        String msg = "";//TODO 还没有模板
        SmsVariableRequest smsVariableRequest = new SmsVariableRequest(account, password, msg, params, report);
        String requestJson = JSON.toJSONString(smsVariableRequest);
        logger.info("smsVariable before request string is: " + requestJson);
        String response = HttpUtils.doPostHttp(variableSend, null, requestJson);
        System.out.println("smsVariable response after request result is : " + response);
        SmsVariableResponse smsVariableResponse = JSON.parseObject(response, SmsVariableResponse.class);
        SmsReturnVo smsReturnVo = new SmsReturnVo();
        smsReturnVo.setContext(msg);
        smsReturnVo.setIsSuccess(false);
        if (smsVariableResponse != null && smsVariableResponse.getCode().equals("0")) {
            smsReturnVo.setIsSuccess(true);
            return smsReturnVo;
        }
        logger.info("smsVariable response  toString is : " + smsVariableResponse);
        return smsReturnVo;
    }


    public static void main(String[] args) {
        String code = SMSUtil.getSmsCode(6);
        String msg = "验证码:+ code 30分钟内有效，请勿向其他人泄露";
        msg = msg.replace("+ code", code);

      /*  String code="123456";
        String msg="【ISTPOS】验证码:"+code+" 30分钟内有效，请勿向其他人泄露";
        String url="http://smssh1.253.com/msg/send/json";
        SmsSendRequest smsSingleRequest = new SmsSendRequest("N2032440", "kr6qsN25z", msg, "xx", report);
        String requestJson = JSONObject.toJSONString(smsSingleRequest);
        System.out.println("sendSms before request string is: " + requestJson);
        String response = HttpUtils.doPostHttp(url, null, requestJson);
        SmsSendResponse smsSingleResponse = JSONObject.parseObject(response, SmsSendResponse.class);
        if (smsSingleRequest != null && smsSingleResponse.getCode().equals("0")) {
            System.out.println("sendSms response after request result is true:" + response);
        }
        System.out.println("sendSms response after request result is :" + response);*/


    }
}
